
#import "TXTelephonyManager.h"
#import <Foundation/Foundation.h>
#import <objc/runtime.h>




@interface CKPreferredServiceManager : NSObject {}
+(id)sharedPreferredServiceManager;
-(BOOL)availabilityForAddress:(id)arg1 onService:(id)arg2 checkWithServer:(BOOL)arg3 ;
@end

@interface IMService : NSObject{}
+(id)iMessageService;

@end

@interface IMDaemonController : NSObject {}
-(BOOL)isConnected;	
+(id)sharedInstance;
+(id)sharedController;
@end

@interface CKIMEntity : NSObject {}
+(id)copyEntityForAddressString:(id)arg1 ;

@end

@interface CKEntity : NSObject {}
+(id)copyEntityForAddressString:(id)arg1 ;
@end

@interface CKConversationList : NSObject {}
+(id)sharedConversationList;
-(id)conversationForExistingChatWithAddresses:(id)arg1 ;
-(id)conversationForRecipients:(id)arg1 create:(BOOL)arg2 ;
// ios 5
- (id)existingConversationForAddresses:(id)arg1;
- (id)conversationForRecipients:(id)arg1 create:(BOOL)arg2 service:(id)arg3;

@end


@interface CKConversation : NSObject {}
-(BOOL)_iMessage_canSendToRecipients:(id)arg1 withAttachments:(id)arg2 alertIfUnable:(BOOL)arg3 ;
-(void)sendMessage:(id)arg1 onService:(id)arg2 newComposition:(BOOL)arg3 ;
-(id)newMessageWithComposition:(id)arg1 ;

@end

@interface CKComposition : NSObject {}
-(id)initWithText:(id)arg1 subject:(id)arg2 ;

@end

///===============================
/// ios 5 
@interface CKMessageStandaloneComposition :NSObject {}
+ (id)newCompositionForText:(id)arg1;

@end

@interface CKMadridEntity :NSObject {}

@end

@interface CKMessageComposition :NSObject {}
+ (id)newCompositionForText:(id)arg1;

@end

@interface CKMadridService :NSObject {}
+ (BOOL)isConnectedToDaemon;
+ (BOOL)isMadridEnabled;
+ (BOOL)isMadridSupported;
- (BOOL)isAvailable;
- (BOOL)ensureMadridConnection;
+ (id)sharedMadridService;
- (int)availabilityForAddress:(id)arg1 checkWithServer:(BOOL)arg2;
- (id)copyEntityForAddressString:(id)arg1;
- (BOOL)canSendToRecipients:(id)arg1 withAttachments:(id)arg2 alertIfUnable:(BOOL)arg3;
- (BOOL)isValidAddress:(id)arg1;
- (id)newMessageWithComposition:(id)arg1 forConversation:(id)arg2;
- (void)sendMessage:(id)arg1;

@end


///===============================
@implementation TXTelephonyManager

/*
*  检查是否是imessage
* -1 0 1 
*/
+ (int)iMessageAvailabilityOfAddress:(NSString *)address{
	if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_5_0 && kCFCoreFoundationVersionNumber <= kCFCoreFoundationVersionNumber_iOS_5_1)
	{
		// ios 5.0 以上 ios5.1 一下的
		CKMadridService *madridService = [objc_getClass("CKMadridService") sharedMadridService];
		return [madridService availabilityForAddress:address checkWithServer:YES ];

	}
	else if (kCFCoreFoundationVersionNumber > kCFCoreFoundationVersionNumber_iOS_5_1)
	{
		// ios 6 ios 7
		CKPreferredServiceManager *preferredServiceManager = [objc_getClass("CKPreferredServiceManager") sharedPreferredServiceManager];

		IMService *service = [objc_getClass("IMService") iMessageService];

		return [preferredServiceManager availabilityForAddress:address onService: service checkWithServer:YES] ;
	}
	else	
	{
		return 0;
	}
}

/***
*  发送imessage 
*/
+ (BOOL)sendMessageWithText:(NSString *)text address:(NSString *)address{

	NSLog(@"address = %@",address);
	
	if (kCFCoreFoundationVersionNumber > kCFCoreFoundationVersionNumber_iOS_6_1){
		// ios 7  
		// NSLog(@"ios 7 kCFCoreFoundationVersionNumber ===== %f ", kCFCoreFoundationVersionNumber);
		IMDaemonController *idcontroller = [objc_getClass("IMDaemonController") sharedController];

		CKEntity *entity = [objc_getClass("CKEntity") copyEntityForAddressString: address];

		CKConversationList *list = [objc_getClass("CKConversationList") sharedConversationList];

		CKConversation *conversation = [list conversationForExistingChatWithAddresses:[NSArray arrayWithObject:address] ]; 

		if (conversation == nil)
		{
			conversation = [list conversationForRecipients:[NSArray arrayWithObject:entity]  create:YES]; 
		}


		BOOL canSend = [conversation _iMessage_canSendToRecipients:entity withAttachments:nil alertIfUnable:NO] ;

		if (canSend && [idcontroller isConnected]){
				
			NSAttributedString *string = [[NSAttributedString alloc] initWithString:text];

			CKComposition *composition = [[objc_getClass("CKComposition") alloc] initWithText:string subject:nil];

			id message = [conversation newMessageWithComposition:composition];

			IMService *service = [objc_getClass("IMService") iMessageService];

			[conversation sendMessage: message onService:service newComposition:NO];

			[string release];
			[composition release];
			[message release];
		}
		[entity release];
			
	}else if(kCFCoreFoundationVersionNumber > kCFCoreFoundationVersionNumber_iOS_5_1 && kCFCoreFoundationVersionNumber <= kCFCoreFoundationVersionNumber_iOS_6_1){
		// ios 6
		// NSLog(@"ios 6 kCFCoreFoundationVersionNumber ===== %f ", kCFCoreFoundationVersionNumber);

		IMDaemonController *idcontroller = [objc_getClass("IMDaemonController") sharedController];

		CKIMEntity *entity = [objc_getClass("CKIMEntity") copyEntityForAddressString: address];

		CKConversationList *list = [objc_getClass("CKConversationList") sharedConversationList];

		CKConversation *conversation = [list conversationForExistingChatWithAddresses:[NSArray arrayWithObject:address] ]; 

		if (conversation == nil)
		{
			conversation = [list conversationForRecipients:[NSArray arrayWithObject:entity]  create:YES]; 
		}


		BOOL canSend = [conversation _iMessage_canSendToRecipients:entity withAttachments:nil alertIfUnable:NO] ;

		if (canSend && [idcontroller isConnected]){
				
			CKMessageComposition *composition = [objc_getClass("CKMessageComposition") newCompositionForText: text];

			id message = [conversation newMessageWithComposition:composition];

			IMService *service = [objc_getClass("IMService") iMessageService];

			[conversation sendMessage: message onService:service newComposition:NO];

			[composition release];
			[message release];
		}
		[entity release];
		
	}else if(kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_5_0 && kCFCoreFoundationVersionNumber <= kCFCoreFoundationVersionNumber_iOS_5_1){
		// ios 5
		// NSLog(@"ios 5 kCFCoreFoundationVersionNumber ===== %f ", kCFCoreFoundationVersionNumber);

    	CKMadridService *madridService = [objc_getClass("CKMadridService") sharedMadridService];
    	if ([objc_getClass("CKMadridService") isConnectedToDaemon] &&
    		[objc_getClass("CKMadridService") isMadridEnabled] &&
    		[objc_getClass("CKMadridService") isMadridSupported] &&
    		[madridService isAvailable] &&
    		[madridService ensureMadridConnection] )
    	{
    		CKMadridEntity *ckEntity = [madridService copyEntityForAddressString: address];

    		if ([madridService canSendToRecipients:[NSArray arrayWithObject:ckEntity] withAttachments:nil  alertIfUnable:NO] &&
    			[madridService isValidAddress:address])
    		{
    			CKConversationList *conversationList = [CKConversationList sharedConversationList];

    			CKConversation *conversation = [conversationList existingConversationForAddresses:[NSArray arrayWithObject:address]];

    			if (conversation == nil)
    			{
    				conversation = [conversationList conversationForRecipients:[NSArray arrayWithObject:ckEntity] create:YES service:madridService];
    			}

				CKMessageStandaloneComposition *composition = [objc_getClass("CKMessageStandaloneComposition") newCompositionForText: text];

				id message = [madridService newMessageWithComposition:composition forConversation:conversation];

				[madridService sendMessage:message];

				[composition release];
    			
    			
    		}

    	}
	}

	return YES;

}


@end